package team.first.mongodb;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanListHandler;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import team.first.mongodb.doc.FilmographyDoc;
import team.first.mongodb.doc.StaffDoc;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.mongodb.entity
 * @Date: 2021/11/18 20:36
 * @Description：
 */
public class MorphiaStaffImport {
    public static final String MONGO_DB_NAME = "movie_db";
    public static final String MONGO_DB_USERNAME = "user";
    public static final String MONGO_DB_PASSWORD = "123456";

    public static void main(String[] args) {
        // Mongo数据库地址
        ServerAddress serverAddress = new ServerAddress("localhost", 27017);

        // Mongo数据库的认证信息
        MongoCredential mongoCredential = MongoCredential.createCredential(MONGO_DB_USERNAME, MONGO_DB_NAME,
                MONGO_DB_PASSWORD.toCharArray());
        MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .applyToClusterSettings(builder ->
                        builder.hosts(Arrays.asList(serverAddress)))
                .credential(mongoCredential).build();

        // MongoClient必须是com.mongodb.client.MongoClient
        MongoClient mongoClient = MongoClients.create(mongoClientSettings);
        Datastore datastore = Morphia.createDatastore(mongoClient, MONGO_DB_NAME);
        datastore.getMapper().mapPackage("team.first.mongodb.doc");


        /* 单个插入测试
        StaffDoc staffDoc = new StaffDoc();
        staffDoc.setId(1008070).setChineseName("基努·李维斯");
        FilmographyDoc filmographyDoc = new FilmographyDoc();
        filmographyDoc.setId(1291843).setOriginalTitle("The matrix").setChineseTitle("黑客帝国");
        List<FilmographyDoc> filmographyDocList = new ArrayList<>();
        filmographyDocList.add(filmographyDoc);
        staffDoc.setActed(filmographyDocList);
        datastore.save(staffDoc);
        */

        // 将数据库中结果插入
        // String sqlGetAllGenres = "SELECT * FROM genre";
        String sqlGetAllStaffs = "select * from staff";
        String sqlGetStaff = "SELECT * FROM staff WHERE id = ?";
        String sqlGetActedMovies = "select movie.*,movie_cast.role_type_id from movie,movie_cast where movie.id = movie_cast.movie_id and staff_id  = ? and role_type_id = 1";
        String sqlGetDirectedMovies = "select movie.*,movie_cast.role_type_id from movie,movie_cast where movie.id = movie_cast.movie_id and staff_id  = ? and role_type_id = 2 ";

        try {
            // 查出所有员工
            List<Entity> staffList = Db.use().query(sqlGetAllStaffs);

            // List<GenreDoc> genreDocList = Db.use().query(sqlGetAllGenres, new BeanListHandler<>(GenreDoc.class));
            // datastore.save(genreDocList);

            staffList.forEach(staff -> {
                try {
                    StaffDoc staffDoc = staff.toBean(StaffDoc.class);

                    List<FilmographyDoc> actedFilms = Db.use()
                            .query(sqlGetActedMovies, new BeanListHandler<>(FilmographyDoc.class), staff.getInt("id"));
                    List<FilmographyDoc> directedFilms = Db.use()
                            .query(sqlGetDirectedMovies, new BeanListHandler<>(FilmographyDoc.class), staff.getInt("id"));
                    staffDoc.setActed(actedFilms).setDirected(directedFilms);
                    datastore.ensureIndexes();

                    datastore.save(staffDoc);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
